////////////////////////////////////////////////////////////////////////////
//
//  Crytek Engine Source File.
//  Copyright (C), Crytek Studios, 2001-2005.
// -------------------------------------------------------------------------
//  File name:   AudioFileCacheNode.cpp
//  Version:     v1.00
//  Created:     Oct 26th 2010 by Sergiy Shaykin
//  Compilers:   Visual Studio.NET 2005
//  Description: 
// -------------------------------------------------------------------------
//  History:
//
////////////////////////////////////////////////////////////////////////////

#include "StdAfx.h"
#include <ISound.h>
#include "FlowBaseNode.h"



class CFlowNode_AudioFileCache : public CFlowBaseNode<eNCT_Instanced>
{

public:
	CFlowNode_AudioFileCache( SActivationInfo * pActInfo ) 
	{
		m_bEnable = false;
	}

	~CFlowNode_AudioFileCache()
	{
	}

	IFlowNodePtr Clone( SActivationInfo * pActInfo )
	{
		return new CFlowNode_AudioFileCache(pActInfo);
	}

	enum INPUTS 
	{
		eIn_GameHintFirst = 0,
		eIn_GameHint1,
		eIn_GameHint2,
		eIn_GameHint3,
		eIn_GameHint4,
		eIn_GameHint5,
		eIn_GameHint6,
		eIn_GameHintLast,
		eIn_Enable,
		eIn_Disable,
	};

	enum OUTPUTS 
	{
	};

	VIRTUAL void GetConfiguration(SFlowNodeConfig& config)
	{
		static const SInputPortConfig inputs[] = 
		{
			InputPortConfig<string> ("agh_GameHint0", _HELP("Game hint"), "Game Hint"),
			InputPortConfig<string> ("agh_GameHint1", _HELP("Game hint"), "Game Hint"),
			InputPortConfig<string> ("agh_GameHint2", _HELP("Game hint"), "Game Hint"),
			InputPortConfig<string> ("agh_GameHint3", _HELP("Game hint"), "Game Hint"),
			InputPortConfig<string> ("agh_GameHint4", _HELP("Game hint"), "Game Hint"),
			InputPortConfig<string> ("agh_GameHint5", _HELP("Game hint"), "Game Hint"),
			InputPortConfig<string> ("agh_GameHint6", _HELP("Game hint"), "Game Hint"),
			InputPortConfig<string> ("agh_GameHint7", _HELP("Game hint"), "Game Hint"),
			InputPortConfig_Void		("Enable", _HELP("Enables functionality")),
			InputPortConfig_Void		("Disable", _HELP("Disables functionality")),
			InputPortConfig_Null()
		};
		static const SOutputPortConfig outputs[] = 
		{
			OutputPortConfig_Null()
		};    
		config.pInputPorts = inputs;
		config.pOutputPorts = outputs;
		config.sDescription = _HELP("Audio File Cache Manager");
		config.SetCategory(EFLN_APPROVED);
	}

	void Enable(SActivationInfo *pActInfo, bool bEnable)
	{
		if (m_bEnable==bEnable)
			return;

		ISoundSystem* pSoundSystem = gEnv->pSoundSystem;
		if(!pSoundSystem)
			return;

		for(int i=eIn_GameHintFirst; i<=eIn_GameHintLast; i++)
		{
			const string& name = GetPortString(pActInfo, i);
			if(!name.empty())
			{
				if (bEnable)
					pSoundSystem->CacheAudioFile(name.c_str(), eAFCT_GAME_HINT);
				else
					pSoundSystem->RemoveCachedAudioFile(name.c_str(), true);
			}
		}
		m_bEnable = bEnable;
	}


	VIRTUAL void Serialize(SActivationInfo* pActInfo, TSerialize ser)
	{
		bool bEnable = m_bEnable;
		CFlowBaseNode<eNCT_Instanced>::Serialize(pActInfo, ser);
		ser.BeginGroup("FlowAudioFileCache");
		ser.Value("enable", bEnable);
		ser.EndGroup();
		if (ser.IsReading())
			Enable(pActInfo, bEnable);
	}


	VIRTUAL void ProcessEvent( EFlowEvent event, SActivationInfo *pActInfo )
	{
		switch (event)
		{
		case eFE_Initialize:
			{
				if(gEnv->IsEditor() && !gEnv->IsEditing())
				{
					Enable(pActInfo, false);
				}
				m_bEnable = false;
			}
			break;
		case eFE_Activate:
			{
				// Enable
				if (IsPortActive(pActInfo, eIn_Enable))
					Enable(pActInfo, true);

				// Disable
				if (IsPortActive(pActInfo, eIn_Disable))
					Enable(pActInfo, false);
			}
			break;
		}
	}

	VIRTUAL void GetMemoryUsage(ICrySizer * s) const
	{
		s->Add(*this);
	}

protected:
	bool m_bEnable;
};


REGISTER_FLOW_NODE("Sound:AudioFileCache", CFlowNode_AudioFileCache);
